package com.midea.ai.air.ui.home.fragment;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.midea.ai.air.R;
import com.midea.ai.air.common.BaseFragment;
import com.midea.ai.air.common.ConsVal;
import com.midea.ai.air.utils.JumpUtils;
import com.midea.ai.air.view.AlertCenterDialog;
import com.midea.ai.air.view.CircleWaveView;

import java.util.Random;

/**
 * Created by ouyangdi on 2016/4/22.
 * 连接家电->连接新家电Fragment
 */
public class AddDeviceConnectDeviceFragment extends BaseFragment {
    public static final int DEVICE_AC = 0;  //空调
    public static final int DEVICE_PUR = 1;  //净化器
    public static final int DEVICE_HUM = 2;  //加湿器
    public static final int DEVICE_DEHUM = 3;  //除湿机

    private int mDevice;  //家电设备

    private ImageView mTvIcon;
    private CircleWaveView mViewWave;
    private TextView mTvDeviceConnecting;
    private TextView mTvConnectingPro;

    ValueAnimator mAnimator;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_add_device_connect_device, container, false);

        mDevice = new Random().nextInt(4);
        initViews(view);
        return view;
    }

    private void initViews(View view) {
        mTvIcon = (ImageView) view.findViewById(R.id.iv_device_icon);
        mViewWave = (CircleWaveView) view.findViewById(R.id.iv_device_wave);
        mTvDeviceConnecting = (TextView) view.findViewById(R.id.tv_device_connecting);
        mTvConnectingPro = (TextView) view.findViewById(R.id.tv_device_connecting_progress);

        switch (mDevice) {
            case DEVICE_AC:
                mTvIcon.setImageResource(R.drawable.air_add_device_ac);
                mTvDeviceConnecting.setText(getString(R.string.air_add_device_connecting) + getString(R.string.air_device_ac));
                mViewWave.setWaveColor(getResources().getColor(R.color.air_device_ac));
                break;

            case DEVICE_PUR:
                mTvIcon.setImageResource(R.drawable.air_add_device_purifier);
                mTvDeviceConnecting.setText(getString(R.string.air_add_device_connecting) + getString(R.string.air_device_pur));
                mViewWave.setWaveColor(getResources().getColor(R.color.air_device_pur));
                break;

            case DEVICE_HUM:
                mTvIcon.setImageResource(R.drawable.air_add_device_hum);
                mTvDeviceConnecting.setText(getString(R.string.air_add_device_connecting) + getString(R.string.air_device_hum));
                mViewWave.setWaveColor(getResources().getColor(R.color.air_device_hum));
                break;

            case DEVICE_DEHUM:
                mTvIcon.setImageResource(R.drawable.air_add_device_dehum);
                mTvDeviceConnecting.setText(getString(R.string.air_add_device_connecting) + getString(R.string.air_device_dehum));
                mViewWave.setWaveColor(getResources().getColor(R.color.air_device_dehum));
                break;
        }

        mAnimator = ValueAnimator.ofInt(0,100);
        mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mTvConnectingPro.setText(animation.getAnimatedValue()+"%");
            }
        });
        mAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                mViewWave.startAnimatorSet();
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mViewWave.stopAnimatorSet();

                if(new Random().nextBoolean()) {
                    //连接成功
                    connectSuccess();
                } else {
                    //连接失败
                    connectFailure();
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        mAnimator.setDuration(5000);
        mAnimator.setInterpolator(new LinearInterpolator());
        mAnimator.start();
    }

    private void connectSuccess() {
        JumpUtils.toPage(getActivity(), getString(R.string.air_add_device_set), getString(R.string.action_bar_back), ConsVal.ACTIVITY_TAG_ADD_DEVICE_SET);
    }

    private void connectFailure() {
        new AlertCenterDialog(getActivity()).builder().setTitle(getString(R.string.air_add_device_connect_failure))
                .setPositiveButton(getString(R.string.air_add_device_connect_try_again), new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //重试
                        mAnimator.start();
                    }
                })
                .setNegativeButton(getString(R.string.dialog_cancel), new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //取消
                    }
                }).show();
    }
}
